package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.TableColumnUserBiz;
import com.egao.orderB.entity.TableColumnUser;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2021-09-01 15:20:33
 */
@Repository
public class TableColumnUserBizImpl extends BaseDao implements TableColumnUserBiz {

  @Override
  public JsonResult list(PageResult2<T> page, TableColumnUser tableColumnUser, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<TableColumnUser> rm = new BeanPropertyRowMapper<TableColumnUser>(TableColumnUser.class);
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.user_id");
    sql.append(",t.column_id,t.show_is,t.remark,t.create_time,t.create_user");
    sql.append(",tc.title,tc.order_num orderNum");
    sql.append(",t.update_time,t.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_table_column_user t ");
    sql.append(" inner join chf_table_column tc on tc.id=t.column_id ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=t.organization_id and sogzn.deleted=0 ");
    sql.append(" where t.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(tableColumnUser)) {
//			if(Utils.isNotEmpty(tableColumnUser.getName())){
//				sql.append(" and instr(t.name,:name)>0 ");
//				map.put("name", tableColumnUser.getName());
//			}
      if (Utils.isNotEmpty(tableColumnUser.getSort()) && Utils.isNotEmpty(tableColumnUser.getOrder())) {
        sql.append(" order by t." + tableColumnUser.getSort() + " " + tableColumnUser.getOrder());
      } else {
        sql.append(" order by tc.order_num asc, t.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<TableColumnUser> list(TableColumnUser tableColumnUser, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<TableColumnUser> rm = new BeanPropertyRowMapper<TableColumnUser>(TableColumnUser.class);
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.user_id");
    sql.append(",t.column_id,t.show_is,t.remark,t.create_time,t.create_user");
		sql.append(",tc.title,tc.order_num orderNum");
    sql.append(",t.update_time,t.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_table_column_user t ");
    sql.append(" inner join chf_table_column tc on tc.id=t.column_id ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=t.organization_id and sogzn.deleted=0 ");
    sql.append(" where t.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(tableColumnUser)) {
      if (Utils.isNotEmpty(tableColumnUser.getUserId())) {
        sql.append(" and t.user_id=:user_id ");
        map.put("user_id", tableColumnUser.getUserId() + "");
      }
      if (Utils.isNotEmpty(tableColumnUser.getSort()) && Utils.isNotEmpty(tableColumnUser.getOrder())) {
        sql.append(" order by t." + tableColumnUser.getSort() + " " + tableColumnUser.getOrder());
      } else {
        sql.append(" order by tc.order_num asc, t.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public TableColumnUser getById(TableColumnUser tableColumnUser) {
    RowMapper<TableColumnUser> rm = new BeanPropertyRowMapper<TableColumnUser>(TableColumnUser.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.user_id");
    sql.append(",t.column_id,t.show_is,t.remark,t.create_time,t.create_user");
		sql.append(",tc.title,tc.order_num orderNum");
    sql.append(",t.update_time,t.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_table_column_user t ");
    sql.append(" inner join chf_table_column tc on tc.id=t.column_id ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=t.organization_id and sogzn.deleted=0 ");
    sql.append(" where t.deleted=0 and t.id=:id ");
    map.put("id", tableColumnUser.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean save(TableColumnUser tableColumnUser, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_table_column_user (");
    sql.append("organization_id,user_id,column_id,show_is,remark");
    sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:user_id,:column_id,:show_is,:remark");
    sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("user_id", tableColumnUser.getUserId() == null ? null : tableColumnUser.getUserId() + "");
    map.put("column_id", tableColumnUser.getColumnId());
    map.put("show_is", tableColumnUser.getShowIs());
    map.put("remark", tableColumnUser.getRemark());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(TableColumnUser tableColumnUser, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_table_column_user set ");
    sql.append("show_is=:show_is ");
    sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("show_is", tableColumnUser.getShowIs());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", tableColumnUser.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(TableColumnUser tableColumnUser, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (tableColumnUser.getId().length() > 0) {
      for (String ID : tableColumnUser.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_table_column_user set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
